<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>后台管理
            <small>用户管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>后台管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="用户编码/用户名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="birthday" label="用户生日" align="center"></el-table-column>
                <el-table-column prop="username" label="用户名称" align="center"></el-table-column>
                <el-table-column prop="telephone" label="联系电话" align="center"></el-table-column>
                <el-table-column prop="gender" label="性别" align="center"></el-table-column>
                <el-table-column prop="remark" label="用户备注" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增用户" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户生日">
                                                <el-date-picker
                                                        v-model="formData.birthday"
                                                        type="date"
                                                        placeholder="选择日期">
                                                </el-date-picker>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="用户姓名">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="不限" value="0"></el-option>
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="用户密码">
                                                <el-input v-model="formData.password"/>
                                            </el-form-item>
                                        </el-col>
                                        <!--<el-col :span="24">
                                            <el-form-item label="用户状态">
                                                <el-input v-model="formData.station"/>
                                            </el-form-item>
                                        </el-col>-->
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="用户状态">
                                                <el-input v-model="formData.station" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="用户备注">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="联系电话">
                                                <el-input v-model="formData.telephone" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色编码</th>
                                            <th>角色名称</th>
                                            <th>角色说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roles" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑检查组" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户生日">
                                                <el-input v-model="formData.birthday"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="用户姓名">
                                                <el-input v-model="formData.username"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="用户性别">
                                                <el-select v-model="formData.gender">
                                                    <el-option label="不限" value="0"></el-option>
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="用户状态">
                                                <el-input v-model="formData.station"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="用户备注">
                                                <el-input v-model="formData.remark" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="联系电话">
                                                <el-input v-model="formData.telephone" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色编码</th>
                                            <th>角色名称</th>
                                            <th>角色说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="roles" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script>
    axios.defaults.withCredentials=true;
    var vue = new Vue({
        el: '#app',
        data: {
            backend_url: "http://localhost:9002",
            activeName:'first',
            pagination: {//分页相关模型数据
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: null//查询条件
            },
            dataList: [],//当前页要展示的分页列表数据
            formData: {},//表单数据
            tableData: [],//新增和编辑表单中对应的检查项列表数据
            roles: [],//新增和编辑表单中检查项对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//增加表单是否可见
            dialogFormVisible4Edit: false,//编辑表单是否可见

            rules: {//校验规则
                code: [{required: true, message: '项目编码为必填项', trigger: 'blur'}],
                name: [{required: true, message: '项目名称为必填项', trigger: 'blur'}]
            }
        },
        //钩子函数，VUE对象初始化完成后自动执行
        created() {
            this.findPage();
        },
        methods: {
            //编辑
            handleEdit() {
                //1.表单校验
                        //2.校验通过-发送post请求
                        axios.post(this.backend_url + "/user/edit?roles="+this.roles, this.formData).then((response) => {
                            if (response.data.flag) {
                                //请求成功
                                this.$message.success(response.data.message);
                            } else {
                                //请求失败
                                this.$message.error(response.data.message);
                            }
                        }).catch((error) => {
                            console.log(error);
                            this.$message.error("网络异常");
                        }).finally(() => {
                            //3.隐藏新增窗口
                            this.dialogFormVisible4Edit = false;
                            //4.刷新数据
                            this.findPage();
                        });
            },
            // 重置表单
            resetForm(formName) {
                //重置表单数据
                this.formData = {};
                this.roles=[];
                if (this.$refs[formName]) { //判断是否有子节点
                    //重置子节点数据
                    this.$refs[formName].resetFields();
                }
            },
            handleAdd() {
                //表单校验

                        //发起http post请求
                axios.post(this.backend_url + "/user/add?roles="+this.roles, this.formData).then((res) => {
                    if (res.data.flag) {
                        this.$message.success(res.data.message);
                        this.findPage();
                    } else {
                        this.$message.success(res.data.message);
                    }
                }).catch((error) => {
                    this.$message.error("网络异常")
                }).finally(() => {
                    this.formData = {}
                    this.dialogFormVisible = false
                }).finally(() => {
                            //刷新数据
                            this.findPage();
                            //隐藏新增窗口
                            this.dialogFormVisible = false;
                        })
            },
            //分页查询
            findPage(pageNo) {
                if (pageNo) {
                    this.pagination.currentPage = pageNo;
                }
                var param = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString,
                }
                axios.get(this.backend_url + "/user/findpage", {params: param}).then((res) => {
                    if (res.data.flag) {
                        this.dataList = res.data.data.rows;
                        this.pagination.total = res.data.data.total;
                    } else {
                        this.$message.error(res.data.rows.message)
                    }
                }).catch((error) => {
                    this.$message.error("网络异常")
                })
            },
            // 重置表单

            // 弹出添加窗口
            handleCreate() {
                //清空表单
                this.resetForm("dialogFormVisible");
                axios.get(this.backend_url + "/role/all").then((res) => {
                    if (res.data.flag) {
                        this.tableData = res.data.data;
                    }
                }).catch((error) => {
                    this.$message.error("查询角色失败");
                })
                //弹出添加窗口
                this.dialogFormVisible = true;
            },
            // 弹出编辑窗口
            handleUpdate(row) {
                //1.弹出编辑窗口
                this.dialogFormVisible4Edit = true;
                //2.根据id查询数据
                //发起http get请求
                axios.get(this.backend_url + "/user/findById?id=" + row.id).then((res) => {
                    if (res.data.flag) {
                        axios.get(this.backend_url + "/role/all").then((respon) => {
                            if (respon.data.flag) {
                                this.tableData = respon.data.data
                                axios.get(this.backend_url + "/role/findByUserRoleId?id=" + row.id).then((resp) => {
                                    if (resp.data.flag) {
                                        this.roles = resp.data.data;
                                    } else {
                                        this.$message.success(res.data.message);
                                    }
                                }).catch((error) => {
                                    this.$message.error("打钩显示异常")
                                })
                            }
                        }).catch((error) => {
                            this.$message.error("查询显示异常")
                        })
                        this.formData = res.data.data;
                    } else {
                        this.$message.success(res.data.message);
                    }
                }).catch((error) => {
                    this.$message.error("网络异常")
                });
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.findPage(currentPage);
            },
            // 删除
            handleDelete(row) {
                //alert(row.id);
                //弹出提示框
                this.$confirm("确定要删除吗？", "提示", {type: "waring"}).then(() => {
                    //点击确定后执行的方法
                    //发起http post请求
                    axios.post(this.backend_url + "/user/delete?id=" + row.id).then((response) => {
                        if (response.data.flag) {
                            //请求成功
                            this.$message.success(response.data.message);
                        } else {
                            //请求失败
                            this.$message.error(response.data.message);
                        }
                    }).catch((error) => {
                        console.log(error);
                        this.$message.error("网络异常");
                    }).finally(() => {
                        //刷新数据
                        this.findPage();
                    });
                });
            }
        }
    })
</script>
</html>